package com.ruoyi.biz.controller;

import com.ruoyi.collect.domain.Works;
import com.ruoyi.collect.service.IWorksService;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 上传作品Controller
 * 
 * @author lpt
 * @date 2025-08-16
 */
@RestController
@RequestMapping("/collect/works")
public class WorksController extends BaseController {
    @Autowired
    private IWorksService worksService;

    /**
     * 查询上传作品列表
     */
    @PreAuthorize("@ss.hasPermi('collect:works:list')")
    @GetMapping("/list")
    public TableDataInfo list(Works works) {
        startPage();
        List<Works> list = worksService.selectWorksList(works);
        return getDataTable(list);
    }

    /**
     * 导出上传作品列表
     */
    @PreAuthorize("@ss.hasPermi('collect:works:export')")
    @Log(title = "上传作品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Works works) {
        List<Works> list = worksService.selectWorksList(works);
        ExcelUtil<Works> util = new ExcelUtil<Works>(Works.class);
        util.exportExcel(response, list, "上传作品数据");
    }

    /**
     * 获取上传作品详细信息
     */
    @PreAuthorize("@ss.hasPermi('collect:works:query')")
    @GetMapping(value = "/{workId}")
    public AjaxResult getInfo(@PathVariable("workId") Integer workId) {
        return success(worksService.selectWorksByWorkId(workId));
    }

    /**
     * 新增上传作品
     */
    @PreAuthorize("@ss.hasPermi('collect:works:add')")
    @Log(title = "上传作品", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Works works) {
        return toAjax(worksService.insertWorks(works));
    }

    /**
     * 修改上传作品
     */
    @PreAuthorize("@ss.hasPermi('collect:works:edit')")
    @Log(title = "上传作品", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Works works) {
        return toAjax(worksService.updateWorks(works));
    }

    /**
     * 删除上传作品
     */
    @PreAuthorize("@ss.hasPermi('collect:works:remove')")
    @Log(title = "上传作品", businessType = BusinessType.DELETE)
    @DeleteMapping("/{workIds}")
    public AjaxResult remove(@PathVariable Integer[] workIds) {
        return toAjax(worksService.deleteWorksByWorkIds(workIds));
    }

    //----------------------------------------------------------

    @GetMapping("/select")
    public List<Works> select() {
        return worksService.findBatch();
    }

    @GetMapping("/select/{id}")
    public Works selectById(@PathVariable("id") Integer id) {
        return worksService.findById(id);
    }

    @GetMapping("/select/page")
     public Object findPage(@RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                           @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize,
                            Works works) {
        return worksService.findByPage(pageNum, pageSize, works);
    }

    @Anonymous
    @DeleteMapping("/delete")
    public String delete(@RequestBody Integer[] ids) {
        return worksService.removeBatch(ids) > 0 ? "批量删除成功" : "批量删除失败";
    }

    @Anonymous
    @DeleteMapping("/delete/{id}")
    public String delete(@PathVariable Integer id) {
        return  worksService.remove(id) > 0 ? "删除成功" : "删除失败";
    }

    @Anonymous
    @PostMapping("/insert")
    public String insert(@RequestBody Works works) {
        return worksService.addid(works) > 0 ? "新增成功" : "新增失败";
    }

    @Anonymous
    @PutMapping("/update")
    public String update(@RequestBody Works works) {
        return worksService.editid(works) > 0 ? "更改成功" : "更改失败";
    }

}
